package com.ecoolex.discount.sales.admin.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.discount.sales.admin.core.AdminConstant;
import com.ecoolex.discount.sales.entity.DiscountBrandWithBLOBs;
import com.ecoolex.discount.sales.entity.DiscountClassify;
import com.ecoolex.discount.sales.entity.MarketingClassifyBrand;
import com.ecoolex.discount.sales.pojo.AddMarketingClassifyBrandDTO;
import com.ecoolex.discount.sales.pojo.QueryMarketingClassifyBrandDTO;
import com.ecoolex.discount.sales.ws.DiscountProductService;
import com.ecoolex.discount.sales.ws.MarketingProductService;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;

@GraphQL2Module(type = Type.Admin,description = "分类下的品牌营销")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_PROJECT_KEY,
	key = MarketingClassifyBrandGraphql.AUTH,
	name = "分类下的品牌营销管理",
	remark = "分类下的品牌营销管理")
public class MarketingClassifyBrandGraphql {

	public static final String AUTH = AdminConstant.AUTH_PROJECT_KEY + "-MARKETING-CLASSIFY-BRAND";
	
	@Reference
	private MarketingProductService marketingProductService;
	
	@Reference
	private DiscountProductService discountProductService;
	
	@TypeField(type = MarketingClassifyBrand.class, field = "discountClassify", description = "折扣分类")
	public DiscountClassify discountClassify(@Source MarketingClassifyBrand source) {
		return discountProductService.getDiscountClassify(source.getClassifyId());
	}
	
	@TypeField(type = MarketingClassifyBrand.class, field = "discountBrand", description = "折扣品牌")
	public DiscountBrandWithBLOBs discountBrand(@Source MarketingClassifyBrand source) {
		return discountProductService.getDiscountBrand(source.getBrandId());
	}
	
	@TypeField(type = MarketingClassifyBrand.class,field = "canEdit",description = "能编辑")
	public boolean canEdit(@Source MarketingClassifyBrand source,AdminMemberAuthority user) {
		return user.auth(AUTH_EDIT);
	}
	
	@TypeField(type = MarketingClassifyBrand.class,field = "canDel",description = "能删除")
	public boolean canDel(@Source MarketingClassifyBrand source,AdminMemberAuthority user) {
		return user.auth(AUTH_DEL);
	}
	
	public static final String AUTH_GET = AUTH + "-GET";
	
	@QueryField(field = "marketingClassifyBrand",description = "分类下品牌营销详情")
	@AdminAuthoritativeRegister(parentKey = AUTH,key = AUTH_GET,name = "分类下品牌营销详情")
	public MarketingClassifyBrand get(@NonNull Integer id,AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return marketingProductService.getMarketingClassifyBrand(id);
	}
	
	public static final String AUTH_QUERY = AUTH + "-QUERY";
	
	@QueryField(field = "marketingClassifyBrandList",description = "分类下品牌营销列表")
	@AdminAuthoritativeRegister(parentKey = AUTH,key = AUTH_QUERY,name = "分类下品牌营销列表")
	public Pagination<MarketingClassifyBrand> query(@NonNull QueryMarketingClassifyBrandDTO query,@NonNull Sorting[] sorts,@NonNull Page page,AdminMemberAuthority user) {
		user.assertAuth(AUTH_QUERY);
		return marketingProductService.queryMarketingClassifyBrand(query,sorts, page);
	}
	
	public static final String AUTH_ADD = AUTH + "-ADD";
	
	@MutationField(field = "addMarketingClassifyBrand",description = "添加分类下品牌营销")
	@AdminAuthoritativeRegister(parentKey = AUTH,key = AUTH_ADD,name = "添加分类下品牌营销")
	public MarketingClassifyBrand add(@NonNull AddMarketingClassifyBrandDTO record,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		return marketingProductService.addMarketingClassifyBrand(record, user.userName());
	}
	
	public static final String AUTH_EDIT = AUTH + "-EDIT";
	
	@MutationField(field = "editMarketingClassifyBrand",description = "编辑分类下品牌营销权重")
	@AdminAuthoritativeRegister(parentKey = AUTH,key = AUTH_EDIT,name = "编辑分类下品牌营销权重")
	public MarketingClassifyBrand edit(@NonNull Integer id,@NonNull Integer weight,AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		MarketingClassifyBrand marketingClassifyBrand = marketingProductService.getMarketingClassifyBrand(id);
		Assert.isTrue(Check.notNull(marketingClassifyBrand),ResultCode.VERIFY.build(1), "分类下品牌营销不存在");
		marketingClassifyBrand.setWeight(weight);
		return marketingProductService.editMarketingClassifyBrand(marketingClassifyBrand, user.userName());
	}
	
	public static final String AUTH_DEL = AUTH + "-DEL";
	
	@MutationField(field = "delMarketingClassifyBrand",description = "删除分类下品牌营销")
	@AdminAuthoritativeRegister(parentKey = AUTH,key = AUTH_DEL,name = "删除分类下品牌营销")
	public boolean delete(@NonNull Integer id,AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		MarketingClassifyBrand marketingClassifyBrand = marketingProductService.getMarketingClassifyBrand(id);
		Assert.isTrue(Check.notNull(marketingClassifyBrand),ResultCode.VERIFY.build(1), "分类下品牌营销不存在");
		marketingProductService.deleteMarketingClassifyBrand(marketingClassifyBrand, user.userName());
		return true;
	}
}
